<template>
  <div id="app">
      <!-- 全局缓存 -->
        <router-view/>
      
    
  </div>
</template>
<script>
    export default {
        name: 'app',
        //让当前项目一进来就可以获取定位
        created() {
            this.getLocation();

        },
        methods: {
            getLocation() {
                const self = this
                AMap.plugin('AMap.Geolocation', function() {
                    var geolocation = new AMap.Geolocation({
                        // 是否使用高精度定位，默认：true
                        enableHighAccuracy: true,
                        // 设置定位超时时间，默认：无穷大
                        timeout: 10000,
                    })

                    geolocation.getCurrentPosition()
                    AMap.event.addListener(geolocation, 'complete', onComplete)
                    AMap.event.addListener(geolocation, 'error', onError)

                    function onComplete(data) {
                        // data是具体的定位信息,complete表示精准定位
                        console.log(data)
                        self.$store.dispatch("setLocation", {
                            //包含城市省份
                            addressComponens: {
                                city: data.addressComponent.city,
                                province: data.addressComponent.province
                            },
                        })
                        self.$store.dispatch("setAddress", data.formattedAddress);
                        //获取定位
                        console.log(data.formattedAddress)
                    }

                    function onError(data) {
                        // 定位出错
                        self.getlatitude()
                    }


                })
            },
            //如果精准定位失败就采用经纬度方法
            getlatitude() {
                const self = this;
                AMap.plugin('AMap.CitySearch', function() {
                    var citySearch = new AMap.CitySearch()
                    citySearch.getLocalCity(function(status, result) {
                        if (status === 'complete' && result.info === 'OK') {
                            // 查询成功，result即为当前所在城市信息
                            console.log(result)
                            AMap.plugin('AMap.Geocoder', function() {
                                var geocoder = new AMap.Geocoder({
                                    // city 指定进行编码查询的城市，支持传入城市名、adcode 和 citycode
                                    city: result.adcode //获取当前ip所在编码
                                })

                                var lnglat = result.rectangle.split(";")[0].split(","); //分解当前经纬度

                                geocoder.getAddress(lnglat, function(status, data) {
                                    if (status === 'complete' && data.info === 'OK') {
                                        // result为对应的地理位置详细信息
                                        // console.log(data);
                                        // data是获取location里面的信息
                                        self.$store.dispatch('setLocation', {
                                            //包含城市省份
                                            addressComponens: {
                                                city: result.city,
                                                province: result.province
                                            },
                                            //精准定位失败后的地址
                                            formattedAddress: data.regeocode.formattedAddress
                                        });
                                        self.$store.dispatch('setAddress', data.regeocode.formattedAddress)
                                    }
                                })
                            })
                        }
                    })
                })
            }
        },
    }
</script>

<style scoped>
    #app {
        width: 100%;
        height: 100%;
        font-size: 14px;
        background: #f1f1f1;
    }
</style>